package com.baoteng.rbspy.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;

import com.baoteng.rbspy.model.response.ResponseModel;
import com.baoteng.rbspy.service.CaptchaService;
import com.baoteng.rbspy.service.OkHttpClientService;
import com.baoteng.rbspy.util.JsonUtils;
import com.baoteng.rbspy.vo.CaptchaVO;
import com.baoteng.rbspy.vo.RecognizeResultVO;
import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;

import okhttp3.RequestBody;

@Service("picsCaptchaService")
public class PicsCaptchaServiceImpl implements CaptchaService{
	
	private static final Logger logger = LoggerFactory.getLogger(PicsCaptchaServiceImpl.class);
	
	@Autowired
	OkHttpClientService okHttpClientService;
	
	@Value("${captcha.api.url:}")
	private String apiUrl;
	
	@Override
	public byte[] build() {
		return null;
	}

	private String requestHelper(Object request) throws Exception {
		String requestBody = JsonUtils.toJsonString(request);
		RequestBody body = RequestBody.create(okhttp3.MediaType.parse(MediaType.APPLICATION_JSON_UTF8_VALUE), requestBody);
		ResponseModel<String> response = okHttpClientService.postHtml(apiUrl, body, String.class);
		return response.getBody();
	}

	@Override
	public CaptchaVO recognition(CaptchaVO captcha) {
		Preconditions.checkNotNull(captcha, "captcha is null");
		Preconditions.checkNotNull(captcha.getData(), "captcha data is null");
		captcha.setType(1);
		try {
			String response = requestHelper(captcha);
			if(StringUtils.isNotBlank(response)){
				RecognizeResultVO result = JsonUtils.parseJson(response,
						new TypeReference<RecognizeResultVO>() {
						});
				if(result!=null && result.getCaptcha()!=null && result.getCaptcha().size()>0 && "0".equals(result.getCaptcha().get(0).getCode())){
					return result.getCaptcha().get(0);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<CaptchaVO> recognition(List<CaptchaVO> captchas) {
		Preconditions.checkNotNull(captchas, "captcha is null");
		Preconditions.checkArgument(captchas.size()>0, "captcha data is null");
		try {
			String response = requestHelper(captchas);
			if(StringUtils.isNotBlank(response)){
				RecognizeResultVO result = JsonUtils.parseJson(response,
						new TypeReference<RecognizeResultVO>() {
						});
				if(result!=null && "0".equals(result.getCode()) && result.getCaptcha()!=null && result.getCaptcha().size()>0){
					List<CaptchaVO> vos = Lists.newArrayList();
					for(CaptchaVO vo : result.getCaptcha()){
						if("0".equals(vo.getCode())) vos.add(vo);
					}
					if(vos.size()!=captchas.size()) {
						logger.info("PicsCaptchaService recognition fail.data-[{}]",JsonUtils.toJsonString(captchas));
						return null;
					}
					return vos;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
